<template>
    <div class="views-shangpin-add">
        <div>
            <el-card class="box-card">
                <template #header>
                    <div class="clearfix">
                        <span class="title"> 添加商品 </span>
                    </div>
                </template>

                <el-form :model="form" ref="formModel" :label-width="labelWidth" status-icon validate-on-rule-change>
                    <el-form-item label="编号 " prop="bianhao" :rules="[{required:true, message:'请填写编号'}]">
                        <el-input type="text" placeholder="输入编号" style="width: 450px" v-model="form.bianhao" />
                    </el-form-item>

                    <el-form-item label="名称 " prop="mingcheng" required :rules="[{required:true, message:'请填写名称'}]">
                        <el-input type="text" placeholder="输入名称" style="width: 450px" v-model="form.mingcheng" />
                    </el-form-item>

                    <el-form-item label="分类 " prop="fenlei" required :rules="[{required:true, message:'请填写分类'}]">
                        <el-select v-model="form.fenlei"><e-select-option type="option" module="shangpinfenlei" value="id" label="fenleimingcheng"></e-select-option></el-select>
                    </el-form-item>

                    <el-form-item label="图片 " prop="tupian"> <e-upload-images v-model="form.tupian" is-paste></e-upload-images> </el-form-item>

                    <el-form-item label="价格 " prop="jiage" required :rules="[{required:true, message:'请填写价格'}, {validator:rule.checkNumber, message:'输入一个有效数字'}]">
                        <el-input type="number" placeholder="输入价格" style="width: 450px" v-model.number="form.jiage" />
                    </el-form-item>

                    <el-form-item label="库存 " prop="kucun" required :rules="[{required:true, message:'请填写库存'}, {validator:rule.checkNumber, message:'输入一个有效数字'}]">
                        <el-input type="number" placeholder="输入库存" style="width: 450px" v-model.number="form.kucun" />
                    </el-form-item>

                    <el-form-item
                        label="库存预警 "
                        prop="kucunyujing"
                        required
                        :rules="[{required:true, message:'请填写库存预警'}, {validator:rule.checkNumber, message:'输入一个有效数字'}]"
                    >
                        <el-input type="number" placeholder="输入库存预警" style="width: 450px" v-model.number="form.kucunyujing" />
                    </el-form-item>

                    <el-form-item label="详情 " prop="xiangqing"> <e-editor v-model="form.xiangqing" @getContent="getxiangqingContent"></e-editor> </el-form-item>

                    <el-form-item v-if="btnText">
                        <el-button type="primary" @click="submit">{{ btnText }}</el-button>
                    </el-form-item>
                </el-form></el-card
            >
        </div>
    </div>
</template>

<script setup>
    import http from "@/utils/ajax/http";
    import DB from "@/utils/db";
    import rule from "@/utils/rule";
    import router from "@/router";
    import EEditor from "@/components/EEditor.vue";

    import { ref, reactive, computed } from "vue";
    import { useRoute } from "vue-router";
    import { session } from "@/utils/utils";
    import { ElMessage, ElMessageBox } from "element-plus";
    import { useShangpinCreateForm, canShangpinInsert } from "@/module";
    import { extend } from "vue-design-plugin";

    const route = useRoute();
    const props = defineProps({
        btnText: {
            type: String,
            default: "保存",
        },
        isRead: {
            type: Boolean,
            default: true,
        },
        isHouxu: {
            type: Boolean,
            default: true,
        },
        labelWidth: {
            type: String,
            default: "140px",
        },
    });
    const { form } = useShangpinCreateForm();
    const emit = defineEmits(["success"]);
    const formModel = ref();
    const loading = ref(false);
    var submit = () => {
        return new Promise((resolve, reject) => {
            formModel.value
                .validate()
                .then((res) => {
                    if (loading.value) return;
                    loading.value = true;
                    canShangpinInsert(form).then(
                        (res) => {
                            loading.value = false;
                            if (res.code == 0) {
                                emit("success", res.data);
                                if (props.isHouxu) {
                                    ElMessage.success("添加成功");
                                    formModel.value.resetFields();
                                    form.bianhao = rule.getID();
                                }
                            } else {
                                ElMessageBox.alert(res.msg);
                            }
                            resolve(res);
                        },
                        (err) => {
                            loading.value = false;
                            ElMessageBox.alert(err.message);
                            reject(err);
                        }
                    );
                })
                .catch((err) => {
                    reject(err);
                });
        });
    };

    const getxiangqingContent = (v) => {
        form.xiangqing = v;
    };
</script>

<style scoped lang="scss">
    .views-shangpin-add {
        padding: 20px;
        background-color: #f0f2f5;
        min-height: calc(100vh - 50px - 30px);

        .box-card {
            border-radius: 8px;
            box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);

            .clearfix {
                font-size: 18px;
                font-weight: 600;
                color: #333;
            }
        }

        .el-form-item {
             margin-bottom: 20px;

             :deep(.el-input__wrapper),
             :deep(.el-select__wrapper),
             :deep(.el-textarea__inner) {
                  border-radius: 4px;
                  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.03);
                  transition: all 0.2s ease-in-out;

                  &:hover {
                     box-shadow: 0 1px 8px rgba(0, 0, 0, 0.08);
                  }
              }
         }

        .el-button {
            border-radius: 4px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border: none;
            color: #fff;
            transition: transform 0.3s, box-shadow 0.3s;

            &:hover {
                transform: translateY(-2px);
                box-shadow: 0 4px 12px rgba(102, 126, 234, 0.4);
            }

            &:active {
                transform: translateY(0);
            }
        }
        
        // Style for the editor component if needed, assuming EEditor uses textarea internally or has a similar structure
        :deep(.w-e-text-container) {
             border-radius: 4px;
             box-shadow: 0 1px 4px rgba(0, 0, 0, 0.03);
             transition: all 0.2s ease-in-out;

             &:hover {
                box-shadow: 0 1px 8px rgba(0, 0, 0, 0.08);
             }
        }

    }
</style>
